package com.le.tester.javaThink.fourth.concurrency;

/**
 * createTime：2021/9/14 14:44
 * description：join方法，等待一段时间直到第二个线程结束才继续执行，可以被中断
 */

class Sleeper extends Thread {
    private int duration;

    public Sleeper(String name, int sleepTime) {
        super(name);
        duration = sleepTime;
        start();
    }

    public void run() {
        try {
            sleep(duration);
        } catch (InterruptedException e) {
            System.out.println(getName() + " was interrupted. " + "isInterrupted(): " + isInterrupted());
            return;
        }
        System.out.println(getName() + " has awakened");
    }
}

class Joiner extends Thread {
    private Sleeper sleeper;

    public Joiner(String name, Sleeper sleeper) {
        super(name);
        this.sleeper = sleeper;
        start();
    }

    public void run() {
        try {
            sleeper.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage());
        }
        System.out.println(getName() + " join completed");
    }

}


public class Joining {
    public static void main(String[] args) {
        Sleeper sleeper = new Sleeper("sleepy", 1500);
        Sleeper grumpy = new Sleeper("grumpy", 1500);

        Joiner dopey = new Joiner("Dopey", sleeper);
        Joiner doc = new Joiner("Doc", grumpy);

        //join中的可中断
        grumpy.interrupt();

    }
}
